package Leetcode;

import java.util.Stack;

/**
 * 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作（push、pop、peek、empty）
 */
public class 力扣232_用栈实现队列 {
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        myQueue.push(2);
        System.out.println(myQueue.peek());
        System.out.println(myQueue.pop());
        System.out.println(myQueue.empty());
        myQueue.push(3);
        System.out.println(myQueue.pop());
        System.out.println(myQueue.pop());
        System.out.println(myQueue.pop());
        System.out.println(myQueue.empty());
    }
}

class MyQueue {
    Stack<Integer> stackOne;
    Stack<Integer> stackTwo;

    public MyQueue() {
        stackOne = new Stack<>();
        stackTwo = new Stack<>();
    }

    public void push(int x) {
        stackOne.push(x);
    }

    public int pop() {
        if (!stackTwo.isEmpty()) {
            return stackTwo.pop();
        } else if (!stackOne.isEmpty()) {
            while (!stackOne.isEmpty()) {
                stackTwo.push(stackOne.pop());
            }
            return pop();
        }
        return -1;
    }

    public int peek() {
        if (!stackTwo.isEmpty()) {
            return stackTwo.peek();
        } else if (!stackOne.isEmpty()) {
            return stackOne.elementAt(0);
        }
        return -1;
    }

    public boolean empty() {
        return stackOne.isEmpty() && stackTwo.isEmpty();
    }
}
